﻿<template>
  <a-modal
    title="新增远程存储"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="存储项名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入存储项名称" v-decorator="['title', {rules: [{required: true, message: '请输入存储项名称！'}]}]" />
        </a-form-item>
        <a-form-item label="主机名或IP地址，建议使用域名" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入主机名或IP地址，建议使用域名" v-decorator="['host', {rules: [{required: true, message: '请输入主机名或IP地址，建议使用域名！'}]}]" />
        </a-form-item>
        <a-form-item label="端口号" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入端口号" style="width: 100%" v-decorator="['port', {rules: [{required: true, message: '请输入端口号！'}]}]" />
        </a-form-item>
        <a-form-item label="访问用户名" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入访问用户名" v-decorator="['user', {rules: [{required: true, message: '请输入访问用户名！'}]}]" />
        </a-form-item>
        <a-form-item label="访问密钥" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入访问密钥" v-decorator="['secretKey', {rules: [{required: true, message: '请输入访问密钥！'}]}]" />
        </a-form-item>
        <a-form-item label="存储基础路径" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入存储基础路径" v-decorator="['basePath', {rules: [{required: true, message: '请输入存储基础路径！'}]}]" />
        </a-form-item>
        <a-form-item label="其它选项" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入其它选项" v-decorator="['extensionOptions']" />
        </a-form-item>
        <a-form-item label="存储类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="请选择存储类型" v-decorator="['storeType', {rules: [{ required: true, message: '请选择存储类型！' }]}]">
            <a-select-option v-for="(item,index) in storeTypeData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {
    RemoteStoreAdd
  } from '@/api/modular/main/RemoteStoreManage'

  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        storeTypeData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
	// 初始化方法
	add (record) {
	this.visible = true
	const storeTypeOption = this.$options
	this.storeTypeData = storeTypeOption.filters['enumData']('StoreType')
	},
	/**
	* 提交表单
	*/
	handleSubmit () {
	const { form: { validateFields } } = this
	this.confirmLoading = true
	validateFields((errors, values) => {
	if (!errors) {
	for (const key in values) {
	// WARN: 有可能导致类型错误，注意
	if ((values[key]) !== null && typeof (values[key]) === 'object') {
	values[key] = JSON.stringify(values[key])
	}
	}
	RemoteStoreAdd(values).then((res) => {
	if (res.success) {
	this.$message.success('新增成功')
	this.confirmLoading = false
	this.$emit('ok', values)
	this.handleCancel()
	} else {
	this.$message.error('新增失败：' + JSON.stringify(res.message))
	}
	}).finally((res) => {
	this.confirmLoading = false
	})
	} else {
	this.confirmLoading = false
	}
	})
	},
	handleCancel () {
	this.form.resetFields()
	this.visible = false
	}
	}
	}
</script>
